﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.stockDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters;
using dhDataAcess.produceDAL.Stock;

namespace dhSystemBLL.produceBLL
{
  public   class packInBLL
    {
      
        private V_packbiaoqianTableAdapter _V_packbiaoqianAdapter = null;
        private V_packbiaoqianTableAdapter V_packbiaoqianAdapter
        {
            get
            {
                if (_V_packbiaoqianAdapter == null) _V_packbiaoqianAdapter = new V_packbiaoqianTableAdapter();
                return _V_packbiaoqianAdapter;

            }
        }

        private packStockInTableAdapter _packStockInAdapter = null;
        private packStockInTableAdapter packStockInAdapter
        {
            get
            {
                if (_packStockInAdapter == null) _packStockInAdapter = new packStockInTableAdapter();
                return _packStockInAdapter;

            }
        }
    
        /// <summary>
        /// 查询包装入库从表
        /// </summary>
        /// <returns></returns>

        public stockDataSet .packStockInDataTable  getpackStockInAll()
        {
            try
            {
                return packStockInAdapter.GetDatapackStockInALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
      
        /// <summary>
        /// 根据入库单号查询视图
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public ViewDataSet.V_packbiaoqianDataTable getV_packbiaoqianAllBybillNo(string billNo)
        {
            try
            {
                return V_packbiaoqianAdapter.GetDataV_packbiaoqianBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询包装入库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public stockDataSet.packStockInDataTable getspackStockInAllByorderDetailNo(string orderDetailNo)
        {
            try
            {
                return packStockInAdapter.GetDatapackStockInByorderDetailNo (orderDetailNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 修改包装入库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int packStockInInsert(stockDataSet.packStockInDataTable pTable)
        {
            packStockInModel packStockInRow = new packStockInModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                 packStockInDAL packStockIn = new packStockInDAL(tn, conn);
                 packStockIn.packStockInInsertII(pTable);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行包装入库主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int packStockInDel(string recordid)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                packStockInDAL packStockIn = new packStockInDAL (tn, conn);
                packStockIn.packStockInDel(recordid);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
